<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 2017/10/15
 * Time: 上午9:59
 */

namespace App\Http\Controllers\Admin;

use App\Models;
use App\Repositories;
use Illuminate\Http\Request;
class MenuController extends AdminController
{
    /**
     * 初始化操作
     * UserController constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->setTemplatePrefix(config('admin.systems.template_prefix', 'admin.').'menu.');
    }

    /**
     * 获取当前控制器服务仓库对象
     * @return mixed
     */
    public function getServiceRepository() : Repositories\Admin\MenuRepository
    {
        if (!($this->serviceRepository instanceof Repositories\Admin\MenuRepository)) {
            $this->serviceRepository = new Repositories\Admin\MenuRepository();
        }

        return $this->serviceRepository;
    }

    /**
     * 列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $pid = $request->route('pid');
        $pid = empty($pid) ? 0 : $pid;
        $list = $this->getServiceRepository()->getMenusList($pid, null);
        $data = $this->getMenuList($list);

        return $this->fetch('index', ['data' => $data, 'pid'=>$pid]);
    }

    /**
     * 获取菜单列表
     * @param $list
     * @return array
     */
    protected function getMenuList($list)
    {
        static $data = [];
        foreach ($list as $key=>$value) {
            $data[count($data)+1] = $value;
            $list2 = $this->getServiceRepository()->getMenusList($value['id'], null);
            if (!empty($list2)) {
                $this->getMenuList($list2);
            }
        }
        return $data;
    }

    /**
     * 更新菜单信息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        $pid = $request->route('pid', 0);
        if (strtolower($request->method()) !== 'post') {
            $id = $request->route('id', 0);
            $detail = $this->getServiceRepository()->getSelectModel();
            $action = 'add';
            if (!empty($id)) {
                $detail = $this->getServiceRepository()->getDetailById($id);
                $action = 'edit';
            }

            return $this->fetch('edit', [
                'metaTitle' => $action == 'edit' ? '更新菜单信息' : '添加菜单信息',
                'postUrl'   => get_admin_url('menu/edit', ['pid'=>$pid, 'id'=>$id]),
                'detail'    => $detail,
                'action'    => $action,
                'pid'       => $pid,
                'id'        => $id,
                'pDetail'   => $this->getServiceRepository()->getDetailById($pid)
            ]);
        }

        $info = $request->input('info', []);
        $info['language'] = 'zh';

        $action = $request->input('action', 'add');

        $this->getServiceRepository()->save($info, $action);

        //  保存对应的权限信息

        $permission = Models\Admin\Permission::where('name', '=', $info['slug'])->first();
        if (empty($permission)) {
            $permission = new Models\Admin\Permission();
        }
        $permission->name = $info['slug'];
        $permission->display_name = $info['name'];
        $permission->description = $info['description'];
        $permission->save();

        return $this->success('更新菜单成功', 'no-refresh');
    }
}